package com.application.mylibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author lixiaoyu
 * @version v1.0
 * @description
 * @since 2017/10/23
 */

public class ISharedPrefUtils implements SharedPreferences {
    boolean exist = false;
    private SharedPreferences mSharedPreferences;
    private Editor mEditor;
    private static final Map<String, SharedPreferences> map = new LinkedHashMap<>();

    /**
     * 构造函数
     */
    public ISharedPrefUtils(Context context, String name) {
        mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public synchronized static ISharedPrefUtils getPref(Context context, String spName) {
        try {
            SharedPreferences localSharePrefManager = map.get(spName);
            if (localSharePrefManager == null) {
                map.put(spName, new ISharedPrefUtils(context, spName));
                localSharePrefManager = map.get(spName);
            }
            return (ISharedPrefUtils) localSharePrefManager;
        } finally {
        }
    }

    /**
     * 单体类获取
     *
     * @return ISharedPref对象
     */
    public synchronized static ISharedPrefUtils getPref(Context context) {
        if (context == null)
            return null;
        String name = context.getPackageName();
        int dotIndex = name.lastIndexOf(".");
        if (dotIndex != -1) {
            name = name.substring(dotIndex + 1, name.length());
        }
        return getPref(context, name);
    }

    /**
     * 写入字符串信息
     *
     * @param key
     * @param value
     */
    public void putStringValue(String key, String value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putString(key, value);
        mEditor.apply();
    }

    /**
     * 获取字符串信息
     *
     * @param key
     * @return
     */
    public String getStringValue(String key) {
        return mSharedPreferences.getString(key, "0");
    }

    /**
     * 写入long型数据
     *
     * @param key
     * @param value
     */
    public void putLongValue(String key, long value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putLong(key, value);
        mEditor.apply();
    }

    /**
     * 获取long型数据
     *
     * @param key
     * @return
     */
    public long getLongValue(String key) {
        return mSharedPreferences.getLong(key, 0);
    }

    /**
     * 写入int数据
     *
     * @param key
     * @param value
     */
    public void putIntValue(String key, int value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putInt(key, value);
        mEditor.apply();
    }

    /**
     * 获取int数据
     *
     * @param key
     * @return
     */
    public int getIntValue(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    /**
     * 写入boolean值
     *
     * @param key
     * @param flag
     */
    public void putBooleanValue(String key, boolean flag) {
        mEditor = mSharedPreferences.edit();
        mEditor.putBoolean(key, flag);
        mEditor.apply();
    }

    public void clearDatas() {
        mSharedPreferences.edit().clear().apply();
    }


    /**
     * 获取boolean值
     *
     * @param key
     * @return
     */
    public boolean getBooleanValue(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    @Override
    public boolean contains(String key) {
        exist = mSharedPreferences.contains(key);
        return exist;
    }

    @Override
    public Editor edit() {
        return mSharedPreferences.edit();
    }

    @Override
    public Map<String, ?> getAll() {
        return mSharedPreferences.getAll();
    }

    @Override
    public boolean getBoolean(String key, boolean defValue) {
        return mSharedPreferences.getBoolean(key, defValue);
    }

    @Override
    public float getFloat(String key, float defValue) {
        return mSharedPreferences.getFloat(key, defValue);
    }

    @Override
    public int getInt(String key, int defValue) {
        return mSharedPreferences.getInt(key, defValue);
    }

    @Override
    public long getLong(String key, long defValue) {
        return mSharedPreferences.getLong(key, defValue);
    }

    @Override
    public String getString(String key, String defValue) {
        return mSharedPreferences.getString(key, defValue);
    }

    @Nullable
    @Override
    public Set<String> getStringSet(String s, Set<String> set) {
        return null;
    }

    @Override
    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {

    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {

    }
}

